package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.CommentBean;
import cst.a931xmc.yuejian20.Bean.RegisterBean;
import cst.a931xmc.yuejian20.Interface.OrderIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.ShowService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CommentModel<T> extends ApiModel implements OrderIface {
    private Retrofit retrofit;
    private ShowService service;

    public CommentModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(ShowService.class);
    }

    @Override
    public void getOrderList(int show_id, final Listener l) {
        Call<List<CommentBean>> call = service.getCommentList(show_id);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getOrderDetail(int comment_id, final Listener l) {
        Call<CommentBean> call = service.getCommentDetail(comment_id);
        call.enqueue(new Callback<CommentBean>() {
            @Override
            public void onResponse(Call<CommentBean> call, Response<CommentBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<CommentBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void addComment(int show_id, int user_id, String user_name, String user_pic, String comment_intro, final Listener l) {
        Call<RegisterBean> call = service.addComment(show_id,user_id,user_name,user_pic,comment_intro);
        call.enqueue(new Callback<RegisterBean>() {
            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
